﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BasicExtensions.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   General Extension Methods
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.AutomationML.Extensions
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    ///   General Extension Methods
    /// </summary>
    public static class BasicExtensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// Enumerate and call action
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="source">
        /// </param>
        /// <param name="action">
        /// </param>
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            // ReSharper disable PossibleMultipleEnumeration
            source.ThrowIfNull("source");

            // ReSharper restore PossibleMultipleEnumeration
            action.ThrowIfNull("action");

            // ReSharper disable PossibleMultipleEnumeration
            foreach (var element in source)
            {
                // ReSharper restore PossibleMultipleEnumeration
                action(element);
            }
        }

        /// <summary>
        /// Raise an Exception for null-values
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="value">
        /// </param>
        /// <param name="variableName">
        /// </param>
        /// <returns>
        /// The T. 
        /// </returns>
        public static T ThrowIfNull<T>(this T value, string variableName) where T : class
        {
            if (value == null)
            {
                throw new NullReferenceException(string.Format("Value is Null: {0}", variableName));
            }

            return value;
        }

        #endregion
    }
}